package com.xaicode.learn.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Locker xaicode@sina.com
 * @version 1.0
 * @date 2021/6/17 0017 21:37
 */
@Slf4j
public class NettyByteBuf01 {

    public static void main(String[] args) {

        /*
         * <pre>
         *      +-------------------+------------------+------------------+
         *      | discardable bytes |  readable bytes  |  writable bytes  |
         *      |                   |     (CONTENT)    |                  |
         *      +-------------------+------------------+------------------+
         *      |                   |                  |                  |
         *      0      <=      readerIndex   <=   writerIndex    <=    capacity
         * </pre>
         *
         * netty byte buffer not need <code>flip()</code>
         */

        // create netty byte buffer with Unpooled
        final ByteBuf byteBuf = Unpooled.buffer(10);

        // fill buffer 10 times and writerIndex=10
        for (int i = 0; i < byteBuf.capacity(); i++) {
            byteBuf.writeByte(i);
        }

        for (int j = 0; j < byteBuf.writerIndex(); j++) {
            log.info("byteBuf index [{}] value [{}], current readIndex is [{}]",
                    j, byteBuf.readByte(), byteBuf.readerIndex());
        }

    }

}
